package com.googlecode.gwtbb.client.only.widget.form.user;

import com.googlecode.gwtbb.client.only.remote.ClientSession;
import com.googlecode.gwtbb.client.only.remote.user.UserServiceProxy;
import com.googlecode.gwtbb.client.only.widget.form.FormWidget;
import com.googlecode.gwtbb.client.only.widget.form.entry.*;
import com.googlecode.gwtbb.client.shared.entity.user.User;
import com.googlecode.gwtbb.client.shared.remote.user.RegisterUserBean;
import com.googlecode.gwtbb.client.shared.validation.Failure;
import com.googlecode.gwtbb.client.shared.validation.FieldId;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.CheckBox;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Jun 26, 2008
 */
public class RegisterForm extends FormWidget {
	public static final String TERMS_AGREEMENT = "termsAgreement";
	private RegisterUserBean registerUserBean = new RegisterUserBean();

	public RegisterForm(ClientSession session) {
		super(session);
		init();
	}

	public List<FormEntry> getFormEntries() {
		List<FormEntry> retVal = new ArrayList<FormEntry>(4);
		retVal.add(createLabeledTextBoxFormEntry(User.USERNAME, 0, null, 0, 0, cmss.fieldLabelUsername()));
		retVal.add(createLabeledTextBoxFormEntry(User.EMAIL, 0, null, 1, 0, cmss.fieldLabelEmail()));
		retVal.add(createLabeledPasswordTextBoxFormEntry(User.PASSWORD, 0, null, 2, 0, cmss.fieldLabelPassword()));
		retVal.add(createLabeledPasswordTextBoxFormEntry(RegisterUserBean.PASSWORD_CONFIRM, 0, null, 3, 0, cmss.fieldLabelPasswordConfirm()));
		HtmledCheckBoxFormEntry termsFormEntry = createHtmledCheckBoxFormEntry(TERMS_AGREEMENT, 0, false, 4, 0, cmss.fieldTermsAgreement());
		retVal.add(termsFormEntry);
		return retVal;
	}

	public String getProcessName() {
		return "register";
	}

	public String getProcessSuccess() {
		return cmss.registerProcessSuccess();
	}

	public String getProcessTitle() {
		return cmss.registerProcessTitle();
	}

	public Map<FieldId, Failure> validate(Map<FieldId, Failure> failures, int index) {
		registerUserBean.setUsername(getValue(User.USERNAME, 0));
		registerUserBean.setEmail(getValue(User.EMAIL, 0));
		registerUserBean.setPassword(getValue(User.PASSWORD, 0));
		registerUserBean.setPasswordConfirm(getValue(RegisterUserBean.PASSWORD_CONFIRM, 0));
		registerUserBean.validate(failures, index);
		boolean termsAgreement = getBooleanValue(TERMS_AGREEMENT, 0);
		if (!termsAgreement)
			failures.put(FieldId.create(TERMS_AGREEMENT, 0), new Failure(smss.required()));
		setValue(User.USERNAME, 0, registerUserBean.getUsername());
		setValue(User.EMAIL, 0, registerUserBean.getEmail());
		setValue(User.PASSWORD, 0, registerUserBean.getPassword());
		setValue(RegisterUserBean.PASSWORD_CONFIRM, 0, registerUserBean.getPasswordConfirm());
		return failures;
	}

	protected void doAction() {
		UserServiceProxy.instance().register(registerUserBean, asyncCallback);
	}
}
